package com.dj.shop.poi.impl;


import com.dj.shop.poi.mineInterface.IFileInterface;
//import com.demo.hospital.common.util.FileUtil;
import com.dj.shop.util.FileUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.io.FileInputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName FileInterfaceImpl
 * @Description TODO
 * @Author pfl
 * @Date 2019/1/11 17:05
 */
public class FileInterfaceImpl implements IFileInterface {
    @Override
    public <T> Set<T> readFileInfo(File file, String[] header,Class<T> clazz) {
        Set<T> set =new HashSet<>();
        try {

//            String fileType = file.getName().substring(file.getName().lastIndexOf("."));
            Workbook workbook = FileUtil.getFileType(new FileInputStream(file), file.getName());
            Sheet sheet = workbook.getSheetAt(0);
            int rowNum = sheet.getLastRowNum();

            for (int i = 1; i <= rowNum; i++) {
                Row row = sheet.getRow(i);
                Cell cel1l = row.getCell(0);
                if(cel1l==null) break;
                T obj = clazz.newInstance();
                Field[] fields = obj.getClass().getDeclaredFields();
                for(int j=0;j<header.length;j++){
                    Cell cell = row.getCell(j);
                    if(cell==null) continue;
                    String fieldName =  header[j];
                    String cellStr = "";
                    switch (cell.getCellType()) {
                        case NUMERIC:
                            Double icell = cell.getNumericCellValue();
                            cellStr = icell.toString();
                            break;
                        case STRING:
                            cellStr = cell.getStringCellValue();
                            break;
                        case FORMULA:
                            cellStr= cell.getCellFormula();
                            break;
                        default:
                            cellStr = null;
                    }
                    for(Field field:fields){
                        field.setAccessible(true);
                       if(fieldName.equals(field.getName())){
                           Type fieldType=field.getGenericType();
                           if("java.lang.Integer".equals(fieldType.getTypeName())) {
                               if(cellStr.contains("."))
                                   cellStr=cellStr.substring(0,cellStr.lastIndexOf("."));
                               field.set(obj, Integer.valueOf(cellStr));
                           }else if("java.lang.Long".equals(fieldType.getTypeName())) {
                               field.set(obj, Long.valueOf(cellStr));
                           }else if("java.lang.Double".equals(fieldType.getTypeName())) {
                               field.set(obj, Double.valueOf(cellStr));
                           }else if("java.util.Date".equals(fieldType.getTypeName())) {
                               field.set(obj, Date.parse(cellStr));
                           }else {
                               field.set(obj, cellStr);
                           }
                       }
                    }
                }
                set.add(obj);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return set;
    }
//    @Override
//    public Set<String> readFileInfo(File file) {
//        Set<String> set = new HashSet<>();
//        try {
//            String fileType = file.getName().substring(file.getName().lastIndexOf("."));
//            Workbook workbook = FileUtils.getFileType(new FileInputStream(file), file.getName());
//            Sheet sheet = workbook.getSheetAt(0);
//            int rowNum = sheet.getLastRowNum();
//            for (int i = 1; i < rowNum; i++) {
//                Row row = sheet.getRow(i);
//                Cell cell = row.getCell(0);
//                String cellStr = "";
//                switch (cell.getCellType()) {
//                    case NUMERIC:
//                        Double icell = cell.getNumericCellValue();
//                        cellStr = icell.toString();
//                        break;
//                    case STRING:
//                        cellStr = cell.getStringCellValue();
//                        break;
//                    default:
//                        cellStr = null;
//                }
//                set.add(cellStr);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return set;
//    }
}
